package top.peng.project.example.chapter4;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import top.peng.project.utils.Utils;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/c4")
@Slf4j
public class ThymeleafController {

    @GetMapping("/index-mv")
    public ModelAndView index1(HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        // 设置跳转的视图 默认映射到 src/main/resources/templates/{viewName}.html
        view.setViewName("index");
        // 设置属性
        view.addObject("title", "我的第一个WEB页面");
        view.addObject("desc", "欢迎进入battcn-web 系统");
        Author author = new Author();
        author.setAge(23);
        author.setEmail("tanpengswpu@163.com");
        author.setName("tanpeng");
        view.addObject("author", author);

        Utils.logInfoPrintRequestInfo(request);

        return view;
    }

    @GetMapping("/index-str")
    public String index2(HttpServletRequest request) {
        // 设置属性
        request.setAttribute("title", "我的第一个WEB页面");
        request.setAttribute("desc", "欢迎进入battcn-web 系统");
        Author author = new Author();
        author.setAge(23);
        author.setEmail("tanpengswpu@163.com");
        author.setName("tanpeng");
        request.setAttribute("author", author);

        Utils.logInfoPrintRequestInfo(request);

        // 返回的 index 默认映射到 src/main/resources/templates/xxxx.html
        return "index";
    }

    @Data
    class Author {
        private int age;
        private String name;
        private String email;
        // 省略 get set
    }
}
